package com.itheima.mp.domain.dto;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.vo.UserVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * PageDTO
 * </p>
 *
 * @author flyduck
 * @since 2025/2/26
 */
@Data
@ApiModel(description = "分页结果")
public class PageDTO<T> {
    @ApiModelProperty("总条数")
    private Long total;

    @ApiModelProperty("总页数")
    private Long pages;

    @ApiModelProperty("集合")
    private List<T> list;

    public static <PO,VO> PageDTO<VO> of(Page<PO> page,Class<VO> clazz){
        PageDTO<VO> voPageDTO = new PageDTO<>();
        voPageDTO.setTotal(page.getTotal());
        voPageDTO.setPages(page.getPages());

        List<PO> records = page.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            List<VO> voList = BeanUtil.copyToList(records, clazz);
            voPageDTO.setList(voList);
        }
        return voPageDTO;
    }

    public static <PO,VO> PageDTO<VO> of(Page<PO> page, Function<PO,VO> function){
        PageDTO<VO> voPageDTO = new PageDTO<>();
        voPageDTO.setTotal(page.getTotal());
        voPageDTO.setPages(page.getPages());

        List<PO> records = page.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            List<VO> voList = records.stream().map(function).collect(Collectors.toList());
            voPageDTO.setList(voList);
        }
        return voPageDTO;
    }
}
